/**
 * Copyright (C) 2014-2019 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * <p>
 * Any modifications to this file must keep this entire header intact.
 */

package com.symaster.common.pixy.image.tiff;

import com.symaster.common.pixy.io.RandomAccessOutputStream;
import com.symaster.common.pixy.string.StringUtils;

import java.io.IOException;

public abstract class AbstractByteField extends TiffField<byte[]> {

    public AbstractByteField(short tag, FieldType fieldType, byte[] data) {
        super(tag, fieldType, data.length);
        this.data = data;
    }

    public byte[] getData() {
        return data.clone();
    }

    public String getDataAsString() {
        return StringUtils.byteArrayToHexString(data, 0, TiffField.MAX_STRING_REPR_LEN);
    }

    protected int writeData(RandomAccessOutputStream os, int toOffset) throws IOException {

        if (data.length <= 4) {
            dataOffset = (int) os.getStreamPointer();
            byte[] tmp = new byte[4];
            System.arraycopy(data, 0, tmp, 0, data.length);
            os.write(tmp);
        } else {
            dataOffset = toOffset;
            os.writeInt(toOffset);
            os.seek(toOffset);
            os.write(data);
            toOffset += data.length;
        }
        return toOffset;
    }
}
